package com.ymttest.business.service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.http.client.ClientProtocolException;

import com.ymatou.iapi.exp.parameter.AddUserCoinByEvalBean;
import com.ymatou.iapi.exp.parameter.AddUserExpBean;
import com.ymatou.iapi.exp.parameter.ChangeUserCoinBean;
import com.ymatou.iapi.exp.parameter.Evaluation;
import com.ymatou.iapi.exp.service.AddUserExpCall;
import com.ymatou.iapi.exp.service.ChangeUserCoinCall;
import com.ymt.core.tool.Logger;
import com.ymttest.common.enums.AccountOperateTypeEnum;
import com.ymttest.database.model.FestivalRateConfig;
import com.ymttest.database.model.UserExp;
import com.ymttest.database.model.UserExpDetail;
import com.ymttest.database.sqlwapper.AccountDefaultWapper;
import com.ymttest.database.sqlwapper.ExpWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

public class ExpCallService {
	public static Integer EvalCoin=10;
	public static Integer OldUser=20238699;
	public static Integer OldSeller=288484 ;
	public static Integer pspRate=1;
	public static Integer oldRate=1;
	
	
	/**设置节日倍率
	 * @param rate 倍率
	 * @param enable 是否启用
	 */
	public static void resetFestivalRateConfig(double rate,boolean enable){
		Logger.comment("设置节日倍率:"+rate+",是否启用:"+enable);
		ExpWapper ew = new ExpWapper();
		ew.deleteFestivalRateConfig(YMTDateUtil.getSysTime("yyyy-MM-dd"));
		FestivalRateConfig frc=new FestivalRateConfig();
		frc.setCreateTime(YMTDateUtil.getDate());
		frc.setDate(YMTDateUtil.getSysTime("yyyy-MM-dd"));
		frc.setRate(rate);
		frc.setEnable(enable);
		frc.setLastUpdateTime(YMTDateUtil.getDate());
		frc.setRemark("autoTest");
		ew.insertFestivalRateConfig(frc);
	}
    /**
     * 使用购买商品初始化经验，最小20
     * @param uid
     * @param exp
     * @throws InterruptedException 
     */
    public static AddUserExpBean resetExp(int uid,int exp1) throws InterruptedException {
        try {
        	Logger.comment(uid+":初始化经验值:"+exp1);
        	ExpWapper ew = new ExpWapper();
        	ew.deleteuserexp(uid);
			ew.deleteuserexpdetail(uid, null, null, null);
        	if(exp1<20){
        		if(exp1==0){
        			return null;
        		}
        		Logger.comment("exp过小,初始化失败");
        		return null;
        	}
        	Thread.sleep(1000);
        	AddUserExpBean adduserexpBean=new AddUserExpBean();
        	AddUserExpCall adduserexpCall=new AddUserExpCall();
			
			adduserexpBean.setUserId(uid);
			adduserexpBean.setBizCode("100102");
			adduserexpBean.setBizNo(YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"));
			Map parameters=new HashMap<>();
			parameters.put("isSubmit", true);
			parameters.put("totalPrice",exp1);
			adduserexpBean.setParameters(parameters);
			adduserexpCall.setData(false,adduserexpBean);
			adduserexpCall.callService();
			return adduserexpBean;
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
		return null;
    }
    /**
     * 使用购买商品增加exp，最小20
     * @param uid
     * @param exp1
     */
    public static AddUserExpBean addExp(int uid,int exp1) {
    	Logger.comment(uid+":添加经验值:"+exp1);
        try {
        	ExpWapper ew = new ExpWapper();
        	AddUserExpBean adduserexpBean=new AddUserExpBean();
        	AddUserExpCall adduserexpCall=new AddUserExpCall();
			adduserexpBean.setUserId(uid);
			adduserexpBean.setBizCode("100102");
			adduserexpBean.setBizNo(YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"));
			Map parameters=new HashMap<>();
			parameters.put("isSubmit", true);
			parameters.put("totalPrice",exp1);
			adduserexpBean.setParameters(parameters);
			adduserexpCall.setData(false,adduserexpBean);
			
			adduserexpCall.callService();
			return adduserexpBean;

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
		return null;
    }
    /**
     * 使用不同类型添加经验值
     * @param uid
     * @param exp1
     * @param type 100101 打开APP获得经验值 100102	购买商品获得经验值 100104	主动评价订单获得经验值 1002021	订单取消扣除经验值（全部） 1002022	订单取消扣除经验值（部分）
     * @return
     * @throws InterruptedException 
     */
    public static AddUserExpBean addExp(int uid,int exp1,int type) throws InterruptedException {
        try {
        	Logger.comment(uid+":增加经验值:"+exp1);
        	ExpWapper ew = new ExpWapper();
        	AddUserExpBean adduserexpBean=new AddUserExpBean();
        	AddUserExpCall adduserexpCall=new AddUserExpCall();
			if(type==100101){
				adduserexpBean.setUserId(uid);
				adduserexpBean.setBizCode("100101");
				adduserexpBean.setBizNo(YMTDateUtil.getSysTime("yyyyMMdd"));
				adduserexpCall.setData(false,adduserexpBean);
				adduserexpCall.callService();
				Thread.sleep(2000);
			}else if(type==100102){
				adduserexpBean.setUserId(uid);
				adduserexpBean.setBizCode("100102");
				adduserexpBean.setBizNo(YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"));
				Map parameters=new HashMap<>();
				parameters.put("isSubmit", true);
				parameters.put("totalPrice",exp1);
				adduserexpBean.setParameters(parameters);
				adduserexpCall.setData(false,adduserexpBean);
				adduserexpCall.callService();
			}
			else if(type==100104){
				adduserexpBean.setUserId(uid);
				adduserexpBean.setBizCode("100104");
				adduserexpBean.setBizNo(YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"));
				Map parameters=new HashMap<>();
				parameters.put("isSubmit", true);
				parameters.put("isContainImageText", true);
				parameters.put("totalPrice",exp1*5);
				adduserexpBean.setParameters(parameters);
				adduserexpCall.setData(false,adduserexpBean);
				adduserexpCall.callService();
			}
			else if(type==1002021){
				AddUserExpBean adduserexpBean1 = ExpCallService.addExp(uid, exp1);
				Thread.sleep(1000);
				adduserexpBean.setUserId(adduserexpBean1.getUserId());
				adduserexpBean.setBizCode("100202");
				adduserexpBean.setBizNo(adduserexpBean1.getBizNo());
				adduserexpCall.setData(false,adduserexpBean);
				adduserexpCall.callService();
			}
			else if(type==1002022){
				//2次部分取消
				AddUserExpBean adduserexpBean1 = ExpCallService.addExp(uid, exp1*2+10);
				Thread.sleep(1000);
				adduserexpBean.setUserId(adduserexpBean1.getUserId());
				adduserexpBean.setBizCode("100202");
				adduserexpBean.setBizNo(adduserexpBean1.getBizNo());
				Map parameters=new HashMap<>();
				parameters.put("specId", YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"));
				parameters.put("price",exp1);
				adduserexpBean.setParameters(parameters);
				adduserexpCall.setData(false,adduserexpBean);
				adduserexpCall.callService();
				parameters.put("specId", YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"));
				adduserexpCall.callService();
			}
			Thread.sleep(1000);
			return adduserexpBean;
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
		return null;
    }
    /**
     * 修改金币
     * @param uid
     * @param bizcode
     * @param bizno
     * @param coin
     * @throws InterruptedException
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static void ChangeUserCoin(int uid,String bizcode,String bizno,int coin) throws InterruptedException, ClientProtocolException, IOException {
    	ChangeUserCoinBean changeusercoinBean=new ChangeUserCoinBean();
    	ChangeUserCoinCall changeusercoinCall=new ChangeUserCoinCall();
    	changeusercoinBean.setBizCode(bizcode);
		//流水号
		changeusercoinBean.setBizNo(bizno);
		changeusercoinBean.setUserId(uid);
		changeusercoinBean.setCoin(coin);
		changeusercoinCall.setData(false,changeusercoinBean);
		changeusercoinCall.callService();
    }
    /**
     * 删除用户金币记录
     * @param userid
     */
    public static void resetCoinAccount(int userid){
    	Logger.comment("重置coin数据:"+userid);
    	ExpWapper ExpWapper=new ExpWapper();
    	ExpWapper.deleteUserCoin(userid, null);
		ExpWapper.deleteUserCoinRecord(userid, null, null, null);
    }
    /**
     * 操作账号金币到指定coin
     * @param userid
     * @param coin
     * @throws IOException 
     * @throws InterruptedException 
     * @throws ClientProtocolException 
     */
    public static void accountCoin(int userid,int coin) throws ClientProtocolException, InterruptedException, IOException{
    	Logger.comment("操作金币账号:"+userid+":到指定coin:"+coin);
    	int nowcoin=ExpCallService.getCurUserCoin(userid);
		ExpCallService.ChangeUserCoin(userid, "200202", YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"), nowcoin);
		ExpCallService.ChangeUserCoin(userid, "200105", YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"), coin);
    }
    /**
     * 获取用户去年和今年的金币数量
     * @param uid
     * @return
     */
    public static Integer getUserCoin(int uid){
    	ExpWapper ExpWapper=new ExpWapper();
		//获取去年和今年的数据
		Integer year=Integer.valueOf(YMTDateUtil.getSysTime("yyyy"));
		int nowcoin=0;
		if(ExpWapper.selectUserCoin(uid, year.toString()).size()>0){
			nowcoin=ExpWapper.selectUserCoin(uid, year.toString()).get(0).getCoin();
		}
		year--;
		if(ExpWapper.selectUserCoin(uid, year.toString()).size()>0){
			nowcoin+=ExpWapper.selectUserCoin(uid,year.toString() ).get(0).getCoin();
		}
		return nowcoin;
	}
    /**
     * 获取今年金币
     * @param uid
     * @return
     */
    public static Integer getCurUserCoin(int uid){
    	ExpWapper ExpWapper=new ExpWapper();
		//获取去年和今年的数据
		Integer year=Integer.valueOf(YMTDateUtil.getSysTime("yyyy"));
		int nowcoin=0;
		if(ExpWapper.selectUserCoin(uid, year.toString()).size()>0){
			nowcoin=ExpWapper.selectUserCoin(uid, year.toString()).get(0).getCoin();
		}
		return nowcoin;
	}
    /**
     * 获取去年金币
     * @param uid
     * @return
     */
    public static Integer getPrevCoin(int uid){
    	ExpWapper ExpWapper=new ExpWapper();
		//获取去年和今年的数据
		Integer year=Integer.valueOf(YMTDateUtil.getSysTime("yyyy"))-1;
		int nowcoin=0;
		if(ExpWapper.selectUserCoin(uid, year.toString()).size()>0){
			nowcoin=ExpWapper.selectUserCoin(uid, year.toString()).get(0).getCoin();
		}
		return nowcoin;
	}
    /**
     * 用于金币估算评价订单可获得的金币数
     * 设置订单数据
     */
    public static void resetOrderCNum(Long OrderId,int num){
    	ExpWapper ExpWapper=new ExpWapper();
    	ExpWapper.deleteOrderCatalogNum(OrderId);
    	ExpWapper.insertOrderCatalogNum(OrderId, num);
	}
    /**
     * 初始化一个默认的AddUserCoinByEvalBean
     * @param userid
     * @return
     */
    public static AddUserCoinByEvalBean initAddusercoinbyevalBean(int userid){
    	AddUserCoinByEvalBean addUserCoinByEvalBean=new AddUserCoinByEvalBean();
    	addUserCoinByEvalBean.setBizNo(YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"));
    	addUserCoinByEvalBean.setUserId(userid);
    	addUserCoinByEvalBean.setSubmit(true);
    	List<Evaluation> evaluations=new ArrayList<>();
    	Evaluation evaluation=new Evaluation();
    	evaluation.setCatalogId(TestDataManager.getRandomUUID().toString());
    	evaluation.setContainImage(true);
    	evaluation.setTextLength(50);
		evaluations.add(evaluation);
		addUserCoinByEvalBean.setEvaluationList(evaluations);
		return addUserCoinByEvalBean;
    }
    /**
     * 初始化一个默认的AddUserCoinByEvalBean
     * @param userid
     * @return
     */
    public static AddUserCoinByEvalBean initAddusercoinbyevalBean(int userid,String bizno){
    	AddUserCoinByEvalBean addUserCoinByEvalBean=new AddUserCoinByEvalBean();
    	addUserCoinByEvalBean.setBizNo(bizno);
    	addUserCoinByEvalBean.setUserId(userid);
    	addUserCoinByEvalBean.setSubmit(true);
    	List<Evaluation> evaluations=new ArrayList<>();
    	Evaluation evaluation=new Evaluation();
    	evaluation.setCatalogId(TestDataManager.getRandomUUID().toString());
    	evaluation.setContainImage(true);
    	evaluation.setTextLength(50);
		evaluations.add(evaluation);
		addUserCoinByEvalBean.setEvaluationList(evaluations);
		return addUserCoinByEvalBean;
    }
    public static void main(String[] args) throws Exception {
    	ExpWapper ExpWapper=new ExpWapper();
    	for(int i=0;i<1000000;i++){
    		ExpWapper.insertOrderCatalogNum(Long.valueOf(i), new Random().nextInt(10));
    	}
    }
}
